package com.tentact.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tentact.service.StuService;
import com.tentact.vo.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 侯炳超
 * @date 2021-12-13 15:51
 */
@WebServlet(urlPatterns = "/StuSelectAllServlet")
public class StuSelectAllServlet extends HttpServlet {
    private StuService service = new StuService();
    public StuSelectAllServlet() {
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String stuname = "";

        List<Student> stus = service.selectByEname(stuname);
        //创建接收容器
        Map<String,Object> map = new HashMap<>();
        map.put("stus",stus);
        //使用json的jar包里的工具类 将对象转换成json对象（字符串）
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(map);
        //设置响应内容的类型
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(json);
        out.flush();
        out.close();



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
